package dao

import (
	"TTMS/model"
	"TTMS/utils"
)

// AddUser 向数据库添加用户
func AddUser(username, password, phoneNumber, imgPath string, userType int64) error {
	//写 sql 语句
	sqlStr := "insert into users (username, pwd, phone_number, user_type, img_path) values (?,?,?,?,?)"
	//执行
	_, err := utils.DB.Exec(sqlStr, username, password, phoneNumber, userType, imgPath)
	if err != nil {
		return err
	}
	return nil
}

// CheckUserNameAndPassword 根据用户名和密码从数据库中查询记录
func CheckUserNameAndPassword(username, password string) *model.User {
	//写 sql 语句
	sqlStr := "select id,username, pwd, phone_number, user_type, img_path from users where username=? and pwd=?"
	//执行
	row := utils.DB.QueryRow(sqlStr, username, password)
	user := &model.User{}
	row.Scan(&user.ID, &user.Username, &user.Password, &user.PhoneNumber, &user.UserType, &user.ImgPath)
	return user
}

// CheckUsername 根据用户名从数据库中查询记录
func CheckUsername(username string) *model.User {
	//写 sql 语句
	sqlStr := "select id,username, pwd, phone_number, user_type, img_path from users where username=?"
	//执行
	row := utils.DB.QueryRow(sqlStr, username)
	user := &model.User{}
	row.Scan(&user.ID, &user.Username, &user.Password, &user.PhoneNumber, &user.UserType, &user.ImgPath)
	return user
}

// GetUserByID 根据ID获取用户信息
func GetUserByID(id int64) *model.User {
	//写 sql 语句
	sqlStr := "select id,username, pwd, phone_number, user_type, img_path from users where id=?"
	//执行
	row := utils.DB.QueryRow(sqlStr, id)
	user := &model.User{}
	row.Scan(&user.ID, &user.Username, &user.Password, &user.PhoneNumber, &user.UserType, &user.ImgPath)
	return user
}
